// Felica 連携サンプルアプリ
// Programed by Kazuyuki Eguchi (http://eguchi.jp/)

package jp.eguchi.android.egg.felica;

import com.felicanetworks.mfc.AppInfo;
import com.felicanetworks.mfc.Felica;
import com.felicanetworks.mfc.FelicaEventListener;
import com.felicanetworks.mfc.FelicaException;
import com.felicanetworks.mfc.PushIntentSegment;
import com.felicanetworks.mfc.PushStartBrowserSegment;
import com.felicanetworks.mfc.PushStartMailerSegment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends Activity implements FelicaEventListener
{
    private final int PUSH_TIMEOUT = 10000;
	
	private Felica felica;

    private Handler handler = new Handler();
	
    private ProgressDialog dialog;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.Button01);
        RadioButton rb1 = (RadioButton)findViewById(R.id.Radio1);
        rb1.setChecked(true);
        
        btn.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v)
			{
				test();
			}
		});

        Context appContext = getApplicationContext();
        
        FelicaServiceConnection felicaConnection = FelicaServiceConnection.getInstance();
        felicaConnection.setContext(appContext);
        felicaConnection.connect();
    }
    
    private void test()
    {
    	Log.d("test","test");

    	try
    	{
    		FelicaServiceConnection mfc = FelicaServiceConnection.getInstance();
    		
    		felica = mfc.getFelica();

    		try
    		{
    			Log.d("test","activateFelica");
    			felica.activateFelica(null,this);
    		}
    		catch(FelicaException ex)
    		{
    			Log.d("test","ID=" + ex.getID());
    		}
    	}
    	catch(Exception ex)
    	{
    		Log.d("test",ex.toString());
    	}
    }
    
    private void test1()
    {
    	Log.d("test","test1");
    	
    	try
    	{
    		try
    		{
    			Log.d("test","open");
    			felica.open();
    		}
    		catch(FelicaException ex)
    		{
    			Log.d("test","ID=" + ex.getID());
    		}
    		
    	    handler.post(new Runnable()
    	    {
    	        public void run()
    	        {
        	    	dialog = ProgressDialog.show(MainActivity.this, "送信中","端末をかざしてください。", true);
    	        }
    	    });
    	    
            long startTime = SystemClock.uptimeMillis();
            boolean success = false;

            while (SystemClock.uptimeMillis() - startTime < PUSH_TIMEOUT)
            {
            	try
            	{
                	RadioButton rbtn1 = (RadioButton)findViewById(R.id.Radio1);
                	RadioButton rbtn2 = (RadioButton)findViewById(R.id.Radio2);
                	RadioButton rbtn3 = (RadioButton)findViewById(R.id.Radio3);
                	EditText text_lon = (EditText)findViewById(R.id.text_lon);
                	EditText text_lat = (EditText)findViewById(R.id.text_lat);
                	

                	// ブラウザ起動してみよう
            		PushStartBrowserSegment pushSegment1 = new PushStartBrowserSegment("http://hoge.com/", null);

            		// メーラーを立ち上げてみましょう
            		String[] to = {"hoge@hoge.com"};
            		String[] cc = {"hogehoge@hoge.com"};
            	
            		PushStartMailerSegment pushSegment2 = new PushStartMailerSegment(to,cc,"題名", "本文", null);

            		// Androidアプリ立ち上げてみよう(Googleナビ)
            		String lon = text_lon.getText().toString();
            		String lat = text_lat.getText().toString();
            		
            		Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("google.navigation:ll=" + lat + "," + lon));
            		PushIntentSegment pushSegment3 = new PushIntentSegment(intent);
            		
            		if(rbtn1.isChecked())
            		{
            			felica.push(pushSegment1);
            		}
            		else if(rbtn2.isChecked())
            		{
            			felica.push(pushSegment2);
            		}
            		else if(rbtn3.isChecked())
            		{
            			felica.push(pushSegment3);
            		}

            		success = true;
            		break;
            	}
            	catch(FelicaException ex)
            	{
        			Log.d("test","ID=" + ex.getID());
            	}
            }	
            
            if(success == true)
            {
            	Log.d("test","send OK");
            }
            else
            {
            	Log.d("test","Send NG");
            }

            handler.post(new Runnable()
    	    {
    	        public void run()
    	        {
        	    	dialog.cancel();
    	        }
    	    });

            try
    		{
    			Log.d("test","close");
    			felica.close();
    		}
    		catch(FelicaException ex)
    		{
    			Log.d("test","ID=" + ex.getID());
    		}
    	
    		try
    		{
    			Log.d("test","inactivateFelica");
    			felica.inactivateFelica();
    		}
    		catch(FelicaException ex)
    		{
    			Log.d("test","ID=" + ex.getID());
    		}
    	}
    	catch(Exception ex)
    	{
    		Log.d("test","Exception:" + ex.toString());
    	}
    }

	public void errorOccurred(int arg0, String arg1, AppInfo arg2)
	{
		Log.d("test","errorOccurred");
	}

	public void finished()
	{
		Log.d("test","finished");

		test1();
	}
}